package com.ficus.road.maintain.third.zh;

import com.alibaba.fastjson.JSONArray;
import com.ficus.road.maintain.core.model.zh.OperationSign;
import com.ficus.road.maintain.core.model.zh.OperationSignZH;
import com.ficus.road.maintain.dao.OperationSignDao;
import com.ficus.road.maintain.util.MyDateUtil;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.List;

/**
 * @author xiawen yang
 * @date 2022/4/2 上午11:30
 */
@Component
public class ZHOperationSign extends BaseZH {

    @Autowired
    private OperationSignDao operationSignDao;

    @PostConstruct
    void init() {
        arg0 = "sanwangSign";
        arg1 = "{\"pageNum\":5}";
        errMsg = "调用中海接口获取运行体征异常!";
        successMsg = "调用中海接口获取运行体征成功!";
    }

    //    @Scheduled(cron = "0/5 * * * * ?", zone = "GMT+8:00")
    @Scheduled(cron = "* 0/30 0-18 * * ?", zone = "GMT+8:00")
//    @PostConstruct
    public void refreshOperationSign() {
        init();
        String signStr = null;
        int i = 0;
        while (i < 10) {
            signStr = getZHData();
            if (ObjectUtils.isNotEmpty(signStr)) {
                break;
            }
            i++;
        }
        if (ObjectUtils.isEmpty(signStr)) {
            return;
        }
        List<OperationSignZH> operationSignZHList = JSONArray.parseArray(signStr, OperationSignZH.class);
        OperationSign operationSign = convert2OperationSign(operationSignZHList);
        operationSignDao.saveOrUpdate(operationSign);
    }

    public OperationSign convert2OperationSign(List<OperationSignZH> zhList) {
        Date today = new Date();
        String id = MyDateUtil.dateFormatStr(today, "yyyy-MM-dd");
        OperationSign operationSign = new OperationSign();
        operationSign.setCreateTime(today);
        operationSign.setId(id);
        zhList.forEach(zh -> {
            switch (zh.getSignsCode()) {
                case "SignDYJG001":
                    operationSign.setRoadIntactRate(zh.getSignsValue());
                    break;
                case "SignDYJG002":
                    operationSign.setRoadCleaningRate(zh.getSignsValue());
                    break;
                case "SignDYJG003":
                    operationSign.setBridgeIntactRate(zh.getSignsValue());
                    break;
                case "SignDYJG004":
                    operationSign.setBridgeInspectionRate(zh.getSignsValue());
                    break;
                case "SignDYJG006":
                    operationSign.setRoadCoverage(zh.getRate() * 100);
                    break;
            }
        });
        return operationSign;
    }
}
